var JsOper=require("../Tool/JsOper.js");
var DataTable=require("../Tool/CDataTableName.js");
var RoomModel={
	UpdateCleanRoom:function(MySql,aCode,aResponse)
	{
		var Param=[aCode];
		MySql.UpdateTable(DataTable.TableRoomInfo,["IS_DIRTY"],[true]," HOTELCODE=? AND HAS_GUEST=TRUE",Param
			,function(Rst)
			{
				if(Rst===false)Rst=-1;
			  	aResponse?aResponse(Rst):0;	
			});
	},

	UpdateRoomStat:function(MySql,aRoomId,aKeyValueArr,aResponse)
	{
		if(aKeyValueArr===null||aKeyValueArr.length<1) 
		{aResponse?aResponse(-1):0;return;}
		var UpdateColArr=[],UpdateValArr=[];
		for( var i = 0; i < aKeyValueArr.length; i++)
		{
			switch(aKeyValueArr[i].k)
			{
			case "dirty":UpdateColArr.push("IS_DIRTY");UpdateValArr.push(aKeyValueArr[i].v);break;
			case "guest":UpdateColArr.push("HAS_GUEST");UpdateValArr.push(aKeyValueArr[i].v);break;
			case "except":UpdateColArr.push("EXCEPT_STATUS");UpdateValArr.push(aKeyValueArr[i].v);break;
			default:aResponse?aResponse(-4):0;return;
			}
		}
		if(UpdateColArr.length<0)
		{aResponse?aResponse(-1):0;return;}
		MySql.UpdateTable(DataTable.TableRoomInfo,UpdateColArr,UpdateValArr,"ID="+aRoomId,false,
			function(Rst)
			{
				console.log("UpdateRoomStat Rst:"+Rst);
				if(Rst===false)Rst=-1;
			  	aResponse?aResponse(Rst):0;	
			});
	},

	GetAvailableRoom:function(MySql,aCode,aTypeId,aBegTime,aEndTime,aResponse)
	{
		var DateFr=JsOper.ToLongDateStr(new Date(aBegTime*1000));
		var DateTo=JsOper.ToLongDateStr(new Date(aEndTime*1000));
		var QueryCmd=['SELECT t1.ID,t1.ZIMMER,t1.IS_DIRTY '];
		QueryCmd.push(' FROM  ',DataTable.TableRoomInfo,' t1 LEFT JOIN ',DataTable.TableGuestCheckIn,' t2 ' );
		QueryCmd.push(' ON t1.ID=t2.ROOM_ID AND ?<t2.ABREISE AND ?>t2.ANREISE ' );
		QueryCmd.push(' WHERE t1.HOTELCODE=? AND t1.TYPEID=? AND IFNULL(t2.ID,0)=0 AND t1.EXCEPT_STATUS=FALSE ' );
		var QueryPara=[DateFr,DateTo,aCode,aTypeId];
		var runCmd=QueryCmd.join("");
	  	MySql.ExecuteQueryCmd(runCmd,QueryPara
	  		,function(data){
	  			if(data===false){
	  				var DataRst={iRst:-1,iRooms:[]};
	  				aResponse?aResponse(DataRst):0;
	  			}
	  			var Rooms=[];
	  			for(var i in data)
		  		{
	                Rooms.push(
	                {
	                    iId :data[i].ID,
	                    iRoomNo :data[i].ZIMMER,
	                    iIsDirty : data[i].IS_DIRTY
	                });
	                
		  		}
		  		var DataRst={iRst:0,iRooms:Rooms};
				aResponse?aResponse(DataRst):0;
			});
	}
};
exports.RoomModel=RoomModel;